import * as prettier from 'prettier';

interface Value {
  value: string | object | Function | [];
}

function isType(type: string) {
  return function (value: Value) {
    return {}.toString.call(value) === `[object ${type}]`;
  }
}

export const isFunction = isType("Function");
export const isString = isType("String");
export const isObject = isType("Object");
export const isArray = isType("Array");

export const isUrl = (str: string) => {
  const reg = /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
  return reg.test(str);
}

export const replaceToUpperCase = (str: string) => {
  // return str.replace('#/definitions/', '').split('.')[1].replace(/^\S/, s => s.toUpperCase());
  return str.replace('#/definitions/', '').replace(/^\S/, s => s.toUpperCase());
}

export const replace = (str: string) => {
  return str.replace('#/definitions/', '');
}

export const format = (fileContent: string) => {
  try {
    return prettier.format(fileContent, {
      parser: 'typescript',
      trailingComma: 'es5',
      tabWidth: 2,
      semi: true,
      singleQuote: true,
      printWidth: 120,
    });
  } catch (e) {
    return fileContent;
  }
}
